﻿define([
    "dojo/_base/declare"
    , "esri/layers/FeatureLayer"
    , "esri/renderers/SimpleRenderer"
], function (declare
    , FeatureLayer, SimpleRenderer) {
    var featureLayer = declare([FeatureLayer], {
            constructor: function (layerUrl, options = {}) {
            },
            setData: function (mapView, layer) {
                var me = this;
                me.mapView = mapView;
                var mapUtils = mapView.mapUtils;
                var options = layer.layer || {};
                var data = options.data || {};
                me.___layer = layer;
                data.where = data.where || '1=1';

                data.geometryType = data.geometryType || mapUtils.Enum.GType.Point;
                me.setDefinitionExpression(data.where + mapUtils.getWhereRandom());

                var defaultSymbol = {}
                defaultSymbol[mapUtils.Enum.GType.Point] = 'common_point';
                defaultSymbol[mapUtils.Enum.GType.Polyline] = 'common_polyline';
                defaultSymbol[mapUtils.Enum.GType.Polygon] = 'common_polygon';

                options.symbol = options.symbol || defaultSymbol[data.geometryType];

                data.renderer = data.renderer || {};

                me._mapView = mapView;
                if (data.renderer.type) {
                    me.__setRenderer(mapView, layer, data.renderer);
                } else if (options.isSetExtent) {
                    me.setExtent(mapView, options);
                } else {
                    if (typeof options.callback == 'function') {
                        options.callback(layer);
                    }
                    ;
                }
                ;
            },
            setExtent: function (mapView, options) {
                var me = this;
                if (!mapView) {
                    return
                }
                ;

                var where = "1=1";
                if (options.data && options.data.where) {
                    where = options.data.where;
                }
                ;

                options.expandFactor = options.expandFactor || 2;

                mapView.zoomToCenterByLayer(me.id, {
                    where: where,
                    zoomType: 'extent',
                    isClick: false,
                    ...options,
                    callback: function (graphics, center) {
                        if (typeof  options.callback == "function") {
                            me.___layer.___data = graphics;
                            me.___layer.___center = center;
                            options.callback(me.___layer);
                        }
                        ;
                    }
                });
            },
            setWhere: function (where) {
                var me = this;
                me.setDefinitionExpression(where + me.mapView.mapUtils.getWhereRandom());
            },
            __setRenderer: function (mapView, layer, options) {
                var me = this;
                var renderer = null;
                var o = layer.layer || {};
                var mapUtils = mapView.mapUtils;
                switch (options.type) {
                    case mapUtils.Enum.RenderType.SimpleRenderer:
                        renderer = mapUtils.getSimpleRenderer(mapView, options.symbol);
                        break;
                    case mapUtils.Enum.RenderType.UniqueValueRenderer:
                        renderer = mapUtils.getUniqueValueRenderer(mapView, options.symbol || options);
                        break;
                    case mapUtils.Enum.RenderType.ClassBreaksRenderer:
                        renderer = mapUtils.getClassBreaksRenderer(mapView, options.symbol || options);
                        break;
                }
                ;
                me.setRenderer(renderer);
                if (o.isSetExtent) {
                    this.setExtent(mapView, o);
                } else {
                    if (typeof o.callback == 'function') {
                        o.callback(layer);
                    }
                    ;
                }

            },
            getGraphicLocation: function (mapView, esriEvent) {
                var me = this;
                var location = null;
                var mapUtils = mapView.mapUtils;
                var _geometryType = esriEvent.graphic.geometry.type;
                switch (_geometryType) {
                    case mapUtils.Enum.GType.Text:
                    case mapUtils.Enum.GType.Point:
                        location = me.getPointGraphicLocation(mapView, esriEvent);
                        break;
                    case mapUtils.Enum.GType.Polyline:
                        location = me.getPolylineGraphicLocation(mapView, esriEvent);
                        break;
                    case  mapUtils.Enum.GType.Extent:
                    case  mapUtils.Enum.GType.Circle:
                    case mapUtils.Enum.GType.Polygon:
                        location = me.getPolygonGraphicLocation(mapView, esriEvent);
                        break;
                }
                ;
                return location;
            },
            getPointGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.graphic) {
                    return;
                }
                var geometry = esriEvent.graphic.geometry || {};

                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {};
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            },
            getPolylineGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.mapPoint) {
                    return;
                }
                var geometry = esriEvent.mapPoint || {};

                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {};
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            },
            getPolygonGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.mapPoint) {
                    return;
                }
                var geometry = esriEvent.mapPoint || {};
                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {}
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            }
        })
    ;
    return featureLayer;
});